package com.dabao.controller.admin;

import com.dabao.bean.Role;
import com.dabao.bean.User;
import com.dabao.controller.BaseController;
import com.dabao.query.Page;
import com.dabao.query.UserQuery;
import com.dabao.service.RoleService;
import com.dabao.service.UserService;
import com.dabao.util.MD5Utils;
import com.dabao.util.web.MsgEnum;
import com.dabao.util.web.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * Created by kakalee on 16/4/24.
 */
@Controller
@RequestMapping("/admin/user/")
public class UserController extends BaseController{

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @RequestMapping("")
    public String index(Model model) {
        List<Role> roles = roleService.selectList(null);
        model.addAttribute("roles", roles);
        return "admin/user/list";
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public String list(@RequestParam(value = "draw", required = false, defaultValue = "1")String draw,
                       @RequestParam(value = "start", required = false, defaultValue = "0") int pageNo,
                       @RequestParam(value = "length", required = false, defaultValue = "0") int pageSize) {
        UserQuery query = new UserQuery();
        query.setOrderColumn("id");
        query.setIsDesc(1);
        query.setPage(Page.getPage(pageNo, pageSize));

        int total = userService.selectCount(query);
        List<User> users = userService.selectList(query);
        return ResponseUtils.listData(total, users, draw);
    }

    @RequestMapping(value = "/save", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String save(@RequestParam(value = "id", required = false)Integer id,
                       @RequestParam("username")String username,
                       @RequestParam("roleId")Integer roleId,
                       @RequestParam("password")String password) {
        User user = new User();
        user.setId(id);
        user.setUsername(username);
        user.setRoleId(roleId);
        user.setPassword(MD5Utils.md5(password));
        user.setType(1);
        user.setCreateTime(new Date());
        int result;
        if(user.getId() == null) {
            result = userService.insert(user);
        }

        else {
            result = userService.update(user);
        }

        if(result > 0) {
            return ResponseUtils.success(MsgEnum.MSG_SUCCESS.getValue());
        }
        logger.info("save template item");
        return ResponseUtils.error(MsgEnum.MSG_FAIL.getValue());
    }

    @RequestMapping(value = "/delete", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String delete(@RequestParam("id")Integer id) {
        userService.delete(id);
        return ResponseUtils.success(MsgEnum.MSG_SUCCESS.getValue());
    }

}
